
var isFromStationClick = false;	
var isToStationClick = false;

$(document).ready(function(){
	$("#add").click(function(){
		$('#content', parent.document).attr("src","sale/edit-sale.action");
		return false;				 
	})
	
	$("#unConfirm").click(function(){
		updateStatus(0,'未确认');
		return false;				 
	})
	$("#confirm").click(function(){
		updateStatus(1,'确认');
		return false;				 
	})
	
	$("#mulDelete").click(function(){
		var idsArray = new Array();
		var rows = $('#saleTable').datagrid('getSelections');
		if(rows.length==0){
			$.messager.alert("提示","请选中数据","warning");
			return false;
		}
		$.messager.confirm("提示！","确定要删除选中的网点托运单?",function(t){ 
			if(!t) return;
			for(var i=0;i<rows.length;i++){
				idsArray.push(rows[i].saleID);
			}	
			var ids= idsArray.join(",");
			$.post(js+"sale/deleteByIDsSale.action",{ids:ids},function(result){
			     if(result.success){
					$('#saleTable').datagrid('reload');
			      }else{
			        $.messager.alert('提示',"删除不成功","error");	
			      }
			},"json");
			
		});
		
		return false;				 
	})
	
	
	
	$('#fromStation').click(function(){
		if(isFromStationClick){
			return;
		}
		isFromStationClick =true;
		var value =$(this).val();
		$('#fromStation').combogrid({
			rownumbers:true,
			pagination:true, 
			panelWidth:500,
			width:313,
			idField:'stationID',
			textField:'stationName',
			url:js+'station/chooseStation.action',
			columns:[[
				{field:'stationCode',title:'网点编号',width:100},
				{field:'stationName',title:'网点名称',width:100},
				{field:'manager',title:'负责人',width:100},
				{field:'contact',title:'联系人',width:100},
				{field:'cityName',title:'地域城市',width:100},
				{field:'telephone',title:'联系方式',width:100},
				{field:'address',title:'地址',width:100},
				{field:'note',title:'备注',width:100}
				
			]],					
			filter:function(q,row){  
				if(row.stationCode.toUpperCase().indexOf(q.toUpperCase())>=0) return true;
			},
			toolbar:[
			 	{text:'清空',iconCls:'icon-no',	handler:function(){ $('#fromStation').combogrid('clear');  $('#fromStationID').val(null); }},
			 	'-',
			 	{text:'确定',iconCls:'icon-ok',handler:function(){$('#fromStation').combogrid('hidePanel'); }}]
			 ,onHidePanel:function(){
					var fromStationID = $('#fromStation').combogrid('getValue');
					var hidden = $('#fromStationID').val();
					if(hidden==fromStationID||fromStationID==''){
						return;
					}
					$('#fromStationID').val(fromStationID);
			    }
		});	
		$('#fromStation').combogrid('setText',value);
	})
	
	
	$('#toStation').click(function(){
		if(isToStationClick){
			return;
		}
		isToStationClick =true;
		var value =$(this).val();
		$('#toStation').combogrid({
			rownumbers:true,
			pagination:true, 
			panelWidth:500,
			width:313,
			idField:'stationID',
			textField:'stationName',
			url:js+'station/chooseStation.action',
			columns:[[
				{field:'stationCode',title:'网点编号',width:100},
				{field:'stationName',title:'网点名称',width:100},
				{field:'manager',title:'负责人',width:100},
				{field:'contact',title:'联系人',width:100},
				{field:'cityName',title:'地域城市',width:100},
				{field:'telephone',title:'联系方式',width:100},
				{field:'address',title:'地址',width:100},
				{field:'note',title:'备注',width:100}
				
			]],					
			filter:function(q,row){  
				if(row.stationCode.toUpperCase().indexOf(q.toUpperCase())>=0) return true;
			},
			toolbar:[
			 	{text:'清空',iconCls:'icon-no',	handler:function(){ $('#toStation').combogrid('clear');  $('#toStationID').val('');  $('#cityName').val('');}},
			 	'-',
			 	{text:'确定',iconCls:'icon-ok',handler:function(){$('#toStation').combogrid('hidePanel'); }}]
			 ,onHidePanel:function(){
					var cityID = $('#toStation').combogrid('getValue');
					var hidden = $('#toStationID').val();
					if(hidden==cityID||cityID==''){
						return;
					}
					$('#toStationID').val(cityID);
			    }
		});	
		$('#toStation').combogrid('setText',value);
	})	
	
	
	
	$("#mulSearch").click(function(){
		var url;
		
		var saleCode=$('#saleCode').val();
		var customer=$('#customer').val();
		var saleDateBegin=$('#saleDateBegin').datetimebox('getValue');
		var saleDateEnd=$('#saleDateEnd').datetimebox('getValue');
		
		var toStationID=$('#toStationID').val();
		var toContact=$('#toContact').val();
		var toContactAddress=$('#toContactAddress').val();
		
		var fromStationID=$('#fromStationID').val();
		var fromContact=$('#fromContact').val();
		var fromContactAddress=$('#fromContactAddress').val();
		
		var handlerTimeBegin=$('#handlerTimeBegin').datetimebox('getValue');
		var handlerTimeEnd=$('#handlerTimeEnd').datetimebox('getValue');
		var operateTimeBegin=$('#operateTimeBegin').datetimebox('getValue');
		var operateTimeEnd=$('#operateTimeEnd').datetimebox('getValue');
		var handlerName=$('#handlerName').val();
		var operatorName=$('#operatorName').val();
		
		url = js+'sale/viewSale.action';
		url += '?saleCode =' + saleCode;
		url += '&customer =' + customer;
		url += '&saleDateBegin =' + saleDateBegin;
		url += '&saleDateEnd =' + saleDateEnd;
		
		url += '&toStationID =' + toStationID;
		url += '&toContact =' + toContact;
		url += '&toContactAddress =' + toContactAddress;
		
		url += '&fromStationID =' + fromStationID;
		url += '&fromContact =' + fromContact;
		url += '&fromContactAddress =' + fromContactAddress;
		
		url += '&handlerTimeBegin =' + handlerTimeBegin;
		url += '&handlerTimeEnd =' + handlerTimeEnd;
		url += '&operateTimeBegin =' + operateTimeBegin;
		url += '&operateTimeEnd =' + operateTimeEnd;
		
		url += '&handlerName=' + handlerName;
		url += '&operatorName=' + operatorName;
		
		$('#saleTable').datagrid({url:url});
 		return false;				 
	})
	
	$("#mulSearchClear").click(function(){
		$("#mulSearchForm").form('clear');
		return false;
	});
	$("#mulSearchToday").click(function(){
		var url;
		url = js+'sale/viewSale.action';
		var todayDate = new Date();
		var tomorrowDay=new Date(todayDate.valueOf()+1000*3600*24);
		var tomorrow=tomorrowDay.getFullYear()+'-'+(tomorrowDay.getMonth()+1)+'-'+tomorrowDay.getDate();
		var today=todayDate.getFullYear()+'-'+(todayDate.getMonth()+1)+'-'+todayDate.getDate();
		
		url += '?handlerTimeBegin =' + today;
		url += '&handlerTimeEnd =' + tomorrow;
		$('#saleTable').datagrid({url:url});
		return false;
	});
	
	
	
	$('#saleTable').datagrid({
		fit:true,
		nowrap: false,
		striped: true,
		collapsible:true,
		url:js+'sale/viewSale.action',
		idField:'saleID',
		columns:[[
			{field:'ck',checkbox:true},
            {title:'托运单编号',field:'saleCode',width:80,sortable:true},
			{title:'托运方',field:'customer',width:120,sortable:true},
			{title:'下单时间',field:'saleDate',width:120,sortable:true},
			{title:'订单状态',field:'statusStr',width:120,sortable:true},
			{title:'发货网点',field:'fromStationName',width:120,sortable:true},
			{title:'发货联系人',field:'fromContact',width:120,sortable:true},
			{title:'发货地址',field:'fromContactAddress',width:120,sortable:true},
			{title:'到货网点',field:'toStationName',width:120,sortable:true},
			{title:'到货联系人',field:'toContact',width:120,sortable:true},
			{title:'到货地址',field:'toContactAddress',width:120,sortable:true},
			{title:'总数量',field:'qtyTotal',width:120,sortable:true},
			{title:'总毛重',field:'grossWeightTotal',width:120,sortable:true},
			{title:'总净重',field:'weightToTal',width:120,sortable:true},
			{title:'总体积',field:'volumeTotal',width:120,sortable:true},
			{title:'总费用',field:'amountTotal',width:120,sortable:true},
			{title:'创建者',field:'handlerName',width:120,sortable:true},
			{title:'创建时间',field:'handlerTime',width:120,sortable:true},
			{title:'最后修改者',field:'operatorName',width:120,sortable:true},
			{title:'最后修改时间',field:'operateTime',width:120,sortable:true}
			
		]],
		pagination:true,
		rownumbers:true,
		onDblClickRow:function(rowIndex, rowData ){
			$('#content', parent.document).attr("src",js+"sale/preUpdateSale.action?saleID="+rowData.saleID);
		}
	});
})
function lookup(value,name){
	var searchUrl;
	if('all'==name){
		searchUrl = js+'sale/viewSale.action?searchInput='+value;
	}else if('saleCode'==name){
		searchUrl = js+'sale/viewSale.action?searchInput='+value+'&searchKind=saleCode';
	}else if('customer'==name){
		searchUrl = js+'sale/viewSale.action?searchInput='+value+'&searchKind=customer';
	}
	$('#saleTable').datagrid({
		 url:searchUrl
    });
}

function updateStatus(status,msg){
	var idsArray = new Array();
	var rows = $('#saleTable').datagrid('getSelections');
	if(rows.length==0){
		$.messager.alert("提示","请选中数据","warning");
		return false;
	}
	$.messager.confirm("提示！","确定将选中的托运单的状态改为"+msg+"?",function(t){ 
		if(!t) return;
		for(var i=0;i<rows.length;i++){
			idsArray.push(rows[i].saleID);
		}	
		var ids= idsArray.join(",");
		$.post(js+"sale/updateStatusByIDsSale.action",{ids:ids,status:status},function(result){
		     if(result.success){
				$('#saleTable').datagrid('reload');
		      }else{
		        $.messager.alert('提示',"修改状态不成功","error");	
		      }
		},"json");
		
	});
		
}